#!/usr/bin/python

import sys
import re
from Bio import SeqIO

tab_file = sys.argv[1]
tf = open(tab_file, "rU")
lines = tf.readlines()
seqid = sys.argv[2]

print ">Features" + "\t" + seqid

for line in lines:
    l = line.split('\t')
    feat = l[0]
    locus_tag = l[1]
    start = l[2]
    stop = l[3]
    frame = l[4]
    gene = l[5]
    protein_id = l[6]
    ec_num = l[7]
    note = l[8]
    bound_moiety = l[9]
    desc = l[10].rstrip()

    if frame == "+":
        if feat == "CDS":
            if gene != "-":
                if note != "-":
                    if ec_num != "-":
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                    else:
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                else:
                    if ec_num != "-":
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
                    else:
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
            else:
                if note != "-":
                    if ec_num != "-":
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                    else:
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                else:
                    if ec_num != "-":
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
                    else:
                        print start + "\t" + stop + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print start + "\t" + stop + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
        elif feat == "tRNA":
            print start + "\t" + stop + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print start + "\t" + stop + "\t" + "tRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "rRNA":
            print start + "\t" + stop + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print start + "\t" + stop + "\t" + "rRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "ncRNA":
            print start + "\t" + stop + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print start + "\t" + stop + "\t" + "ncRNA" + "\n" + \
                "\t" + "\t" + "\t" + "ncRNA_class" + "\t" + note + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "tmRNA":
            print start + "\t" + stop + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print start + "\t" + stop + "\t" + "tmRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "misc_binding":
            print start + "\t" + stop + "\t" + "misc_binding" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note + "\n" + \
                "\t" + "\t" + "\t" + "bound_moiety" + "\t" + bound_moiety
        elif feat == "misc_feature":
            print start + "\t" + stop + "\t" + "misc_feature" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note
        elif feat == "pseudo":
            print start + "\t" + stop + "\t" + "pseudo" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note
    elif frame == "-":
        if feat == "CDS":
            if gene != "-":
                if note != "-":
                    if ec_num != "-":
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                    else:
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                else:
                    if ec_num != "-":
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
                    else:
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
            else:
                if note != "-":
                    if ec_num != "-":
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                    else:
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag + "\n" + \
                            "\t" + "\t" + "\t" + "note" + "\t" + note
                else:
                    if ec_num != "-":
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "EC_number" + "\t" + ec_num + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
                    else:
                        print stop + "\t" + start + "\t" + "gene" + "\n" + \
                            "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
                        print stop + "\t" + start + "\t" + "CDS" + "\n" + \
                            "\t" + "\t" + "\t" + "product" + "\t" + desc + "\n" + \
                            "\t" + "\t" + "\t" + "protein_id" + "\t" + "gnl|ASGPB|" + locus_tag
        elif feat == "tRNA":
            print stop + "\t" + start + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "gene" + "\t" + gene + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print stop + "\t" + start + "\t" + "tRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "rRNA":
            print stop + "\t" + start + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print stop + "\t" + start + "\t" + "rRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "ncRNA":
            print stop + "\t" + start + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print stop + "\t" + start + "\t" + "ncRNA" + "\n" + \
                "\t" + "\t" + "\t" + "ncRNA_class" + "\t" + note + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "tmRNA":
            print stop + "\t" + start + "\t" + "gene" + "\n" + \
                "\t" + "\t" + "\t" + "locus_tag" + "\t" + locus_tag
            print stop + "\t" + start + "\t" + "tmRNA" + "\n" + \
                "\t" + "\t" + "\t" + "product" + "\t" + desc
        elif feat == "misc_binding":
            print stop + "\t" + start + "\t" + "misc_binding" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note + "\n" + \
                "\t" + "\t" + "\t" + "bound_moiety" + "\t" + bound_moiety
        elif feat == "misc_feature":
            print stop + "\t" + start + "\t" + "misc_feature" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note
        elif feat == "pseudo":
            print start + "\t" + stop + "\t" + "pseudo" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note
    else:
        if feat == "repeat_region":
            print start + "\t" + stop + "\t" + "repeat_region" + "\n" + \
                "\t" + "\t" + "\t" + "note" + "\t" + note

tf.close()